﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES._wangweihao;
using _2112A_2_MES._XiangHe;
using _2112A_2_MES.SuJiaHaos;
using _2112A_2_MES.WangWeiHaos.Outputs;
using _2112A_2_MES.XiangHes;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.SuJiaHao
{
    /// <summary>
    /// 客户服务实现
    /// </summary>
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class CustomerService : ApplicationService, ICustomerService
    {
        /// <summary>
        /// 学生仓储
        /// </summary>
        private readonly IRepository<Client, int> _clientRepository;


        public CustomerService(IRepository<Client, int> clientRepository)
        {
            _clientRepository = clientRepository;
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <returns>返回客户信息的实体</returns>
        public async Task<CustomerManagementDTO> GetAsync(int id)
        {
            var client = await _clientRepository.GetAsync(id);
            return ObjectMapper.Map<Client, CustomerManagementDTO>(client);
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="clientCode">客户编码</param>
        /// <param name="clientName">客户名称</param>
        /// <param name="clientNick">客户简称</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="pageIndex">当前页索引</param>
        /// <returns>返回客户信息的泛型集合</returns>
        public async Task<ReturnSelect<object>> GetCustomerList(string? clientCode, string? clientName, string? clientNick, int pageSize, int pageIndex)
        {
            // 获取客户列表
            var clientList = await _clientRepository.GetListAsync();

            // 使用 WhereIf 进行条件筛选
            var filteredClients = clientList
                .WhereIf(!string.IsNullOrEmpty(clientCode), x => x.ClientCode.Contains(clientCode))
                .WhereIf(!string.IsNullOrEmpty(clientName), x => x.ClientName.Contains(clientName))
                .WhereIf(!string.IsNullOrEmpty(clientNick), x => x.ClientNick.Contains(clientNick))
                .ToList();

            // 计算总记录数和当前页记录
            var totalCount = filteredClients.Count;
            var pageList = filteredClients.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<Client>(pageList);

            // 返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 客户信息添加
        /// </summary>
        /// <param name="input">客户实体</param>
        /// <returns>添加结果，成功返回1，失败返回0</returns>
        public async Task<int> GetCreateCustomer(CreateCustomerDTO input)
        {
            var customer = ObjectMapper.Map<CreateCustomerDTO, Client>(input);
            customer.CreatedBy = "崔二强";
            customer.CreatedTime = DateTime.Now;
            await _clientRepository.InsertAsync(customer);
            return customer != null ? 1 : 0;
        }

        /// <summary>
        /// 客户信息删除
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DeletedCustomer(int id)
        {
            var customerDel=await _clientRepository.GetAsync(id);
            await _clientRepository.DeleteAsync(customerDel);
            return customerDel != null ? 1 : 0;
        }

        /// <summary>
        /// 客户信息反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回客户信息实体</returns>
        public async Task<Client> GetCustomers(int id)
        {
            var customers=await _clientRepository.FirstOrDefaultAsync(x => x.Id == id);
            return customers;
        }

        /// <summary>
        /// 客户信息修改
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateCustomer(CreateCustomerDTO input)
        {
            var customers= ObjectMapper.Map<CreateCustomerDTO, Client>(input);
            customers.UpdatedBy = "崔大强";
            customers.UpdatedTime = DateTime.Now;
            await _clientRepository.UpdateAsync(customers);
            return customers != null ? 1 : 0;
        }

    }
}
